
const fs = require('fs');
const path = require('path');
const eachSeries = require('async/eachSeries');
const log = require('fancy-log');
const fsextra = require('fs-extra');


const assets_dir = path.resolve(__dirname, '../../laya/assets/');
const entry_dir = path.join(assets_dir, '/assets/role');

let argv = require('minimist')(process.argv.slice(2));

let data_map = {};


if (argv.start) {
  traversalDir(entry_dir);
}

async function traversalDir(dir) {
  const all_role_files = fs.readdirSync(dir);
  eachSeries(all_role_files, (file_name, callback)=> {
    if (!/^\d+$/.test(file_name)) return callback();
    let role_no = file_name;
    const filedir = path.join(dir, role_no);
    const stats = fs.statSync(filedir);
    if (!stats.isDirectory()) return callback();
    getRoleAsset(role_no).then(callback);
  }, error=> {
    fs.writeFileSync(
      path.resolve(__dirname, '../data/role_assets.json'),
      JSON.stringify(data_map, null, "\t") 
    );
  });
};


async function getRoleAsset(role_no) {
  return new Promise(resolve=> {
    let role_asset = data_map[role_no] = {};
    const role_dir = path.join(entry_dir, `/${role_no}/`);
    const role_dir_ls = fs.readdirSync(role_dir);

    let defult_assets = {};
    eachSeries(role_dir_ls, (file_name, callback)=> {
      if (role_no==0) {
        let skin_assets = role_asset = data_map[role_no] = [];
        getFragment('', role_dir, '', { skin_assets } ).then(callback);
        return;
      } 
      if (!/^\d+$/.test(file_name)) return callback();
      let skin_no = file_name;
      let skin_assets = role_asset[skin_no] = [];
      getFragment(skin_no, role_dir, '', { skin_assets, defult_assets }).then(callback);
    }, error=> {
      resolve();
    });
  });
};


async function getFragment(skin_no, role_dir, read_dir, extras) {
  let { skin_assets, defult_assets, frament_name } = extras;

  let role_assets_dir = path.join(role_dir, `/${skin_no}/`),
    frament_dir = path.join(role_assets_dir, read_dir);
  let frament_dir_ls =  fs.readdirSync(frament_dir);

  return new Promise(resolve=> {
    if (frament_dir_ls.length===0) { // 空目录
      let data_str = JSON.stringify(defult_assets[frament_name], null, "\t");
      let data_dir = path.join(frament_dir, 'skin_defult.json');
      fs.writeFileSync(data_dir, data_str);
      let url = path.relative(assets_dir, data_dir);
      skin_assets.push({ url, type: 'json' });
      resolve();
    } else {
      eachSeries(frament_dir_ls, (file_name, callback)=> {
        const filedir = path.join(frament_dir, file_name);
        const stats = fs.statSync(filedir);
        if (stats.isFile()) {
          let type = '';
          if (/\.png$/i.test(file_name)) {
            type = 'image';
          } else if (/\.sk$/i.test(file_name)) {
            type = 'arraybuffer';
          } else if (/skin_defult\.json$/i.test(file_name)) {
            type = 'json';
          } else if (/\.json$/i.test(file_name)) {
            type = 'atlas';
          }
          if (type) {
            let url = path.relative(assets_dir, filedir).replace(/\\/g,'/');
            log.info(url);
            skin_assets.push({ url, type });
            if (skin_no==1) {
              defult_assets[frament_name].push({ url, type });
            }
          }
          callback();
        } else if (stats.isDirectory()) {
          extras.frament_name = file_name;
          let read_dir_new = read_dir + '/' + file_name;
          if (skin_no==1) defult_assets[file_name] = [];
          getFragment(skin_no, role_dir, read_dir_new, extras).then(callback);
        }
      }, error=> {
        resolve();
      });
    }
  });
};

